<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class AdController extends BaseController {

	public function saves(){

		$rules = array(
			array('thumbimg','require','广告图地址不能为空！'),
			array('jumpaddres','require','新链接地址不能为空！'),
			array('breplace','require','被替换图片地址不能为空！'),
		);
		$submenu = D(C('TABLE_SUBMENU'));
		if(!$submenu->validate($rules)->create()){
			$this->error($submenu->getError(),'',1);
		}else{
			$submenu->thumbimg = I('post.thumbimg');
			$submenu->jumpaddres = I('post.jumpaddres');
			$where['thumbimg'] = I('post.breplace');
			$result = $submenu->where($where)->save();
			if($result !== false){
				//A(C('TABLE_LOG'))->index(1,'添加文档成功');
				$this->success('执行成功！','/Admin/Index/main');
			}else{
				//A(C('TABLE_LOG'))->index(0,'添加文档失败');
				$this->error('执行失败！','',1);
			}
		}
	}
	
	/*
	 * 广告管理列表
	 */
	public function adlist(){
		
		$this->assign('ad_list',$this->getadlist());
		$this->display();
	}
	
	/*
	 * 获取广告列表信息
	 */
	protected function getadlist(){
		$ad = D(C('TABLE_AD'));
		$res = $ad->order('id desc')->select();
		return $res;
	}
	
	public function adadd(){

		$this->assign('typelist',$this->getmalist());
		$this->display();
	}
	
	public function adedit(){
		$id = I('get.id',0);
		$ad = D(C('TABLE_AD'));
		$result = $ad->find($id);
		$this->assign('adresult',$result);
		$this->assign('typelist',$this->getmalist());
		$this->display();
	}
	
	protected function getmalist(){
		$type = D(C('TABLE_TYPE'));
		$res = $type->where(array('state'=>1))->select();
		return $res;
	}
	
	/*
	 * 添加广告
	 */
	public function add(){
		$this->setadinfo('add');
	}
	
	/*
	 * 编辑广告
	 */
	public function edit(){
		$this->setadinfo('save',I('get.id',0));
	}
	
	protected function setadinfo($methed,$id=null){
		$rules = array(
			array('ad_name','require','广告名称不能为空！'),
			array('ad_img','require','广告图不能为空！'),
			array('ad_link','require','链接地址不能为空！'),
		);
		$ad = D(C('TABLE_AD'));
		if(!$ad->validate($rules)->create()){
			$this->error($ad->getError(),'',1);
		}else{
			$ad_push = I('post.ad_push');
			if($ad_push!=1){
				$ad->ad_push = 0;
			}else{
				$ad->ad_push = 1;
			}
			$ad_isclose = I('post.ad_isclose');
			if($ad_isclose==0){
				$ad->ad_isclose = 0;
			}
			if($methed=='add'){
				$result_num = $ad->add();
			}else{
				$ad->where(array('id'=>$id))->save();
				$result_num = $id;
			}
			$ad_push_ma = I('post.ad_push_ma');
			if(!empty($result_num)){
				/*如果不是全部推送*/
				if($ad_push==1){
					$this->shellhandle($result_num,$ad_push_ma);
				}
				$this->success('成功！','/Admin/Ad/adlist');
			}else if($result_num==0){
				$result_num = $id;
				if($ad_push==1){
					$this->shellhandle($result_num,$ad_push_ma);
				}
				$this->success('成功！','/Admin/Ad/adlist');
			}else{
				$this->error('失败！','',1);
			}
		}
	}
	
	protected function shellhandle($result_num,$ad_push_ma){
		$ad = D(C('TABLE_AD'));
		foreach ($ad_push_ma as $maid){
			//echo $maid."<hr />";
			if(!$this->bindadbytype($result_num, $maid)){
				$this->error("推送失败(".$maid.")！",'',3);
			}
		}
	}
	
	/*
	 * 编辑“码”的广告id
	 */
	protected function bindadbytype($adid,$maid){
		$adid_result = null;
		$getarrayifno = null;
		if(!empty($adid) && !empty($maid)){
			$type = D(C('TABLE_TYPE'));
			/*获取码的广告id*/
			$gettypebyadid = $type->find($maid);
			$idarray = explode(',', $gettypebyadid['adid']);
			if(empty($gettypebyadid['adid'])){
				$adid_result = $adid.",";
			}else{
				foreach ($idarray as $v){
					if(!empty($v)){
						if($v!=$adid){
							$getarrayifno .= $v.',';
						}
					}
				}
				$adid_result = $getarrayifno.$adid.',';
			}
			$data['adid'] = $adid_result;
			$res = $type->where(array('id'=>$maid))->save($data);
			if($res){
				return true;
			}else if($res==0){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
			//$this->error('参数错误！','',1);
		}
		
	}


}